package com.itlong.cloud.property.service.impl;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.itlong.cloud.POJO.DTO.property.PropertyProjectUpdateArrearageLinkageDTO;
import com.itlong.cloud.POJO.PO.DeviceBusinessChangeLogPO;
import com.itlong.cloud.POJO.VO.property.PropertyArrearageLinkageVO;
import com.itlong.cloud.constants.RabbitMQConstant;
import com.itlong.cloud.enumerate.SmartCommunityEnum;
import com.itlong.cloud.property.dao.IPropertyArrearageLinkageDao;
import com.itlong.cloud.property.service.IPropertyArrearageLinkageService;
import com.itlong.cloud.rabbitmq.config.MessageSender;
import com.itlong.cloud.utils.data.handler.SqlUtil;
import org.slf4j.Logger;import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <desc>
 *     收费管理-收费规则处理接口实现.
 * </desc>
 *
 * @createDate 2019-01-22
 */
@Service
public class PropertyArrearageLinkageServiceImpl implements IPropertyArrearageLinkageService {
    private static final Logger LOG = LoggerFactory.getLogger(PropertyArrearageLinkageServiceImpl.class); //日志对象

    @Autowired
    IPropertyArrearageLinkageDao iPropertyArrearageLinkageDao;

    @Autowired
    private MessageSender messageSender;

    /**
     * <desc>
     *    获取欠费联动设置信息
     * <desc/>
     *
     * @param  projectId  项目编号
     * @return  PropertyArrearageLinkageVO 欠费联动信息
     * @author Qiang.S
     * @createDate 2019/01/22
     */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public PropertyArrearageLinkageVO getArrearageLinkageInfo(String projectId) throws Exception {
        return iPropertyArrearageLinkageDao.getArrearageLinkageInfo(projectId);
    }

    /**
     * <desc>
     *      更新欠费联动信息
     * <desc/>
     *
     * @param dto 欠费联动信息
     * @return 受影响的行数
     * @author Qiang.S
     * @createDate 2019/01/22
     */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public PropertyArrearageLinkageVO updateArrearageLinkageInfo(PropertyProjectUpdateArrearageLinkageDTO dto) throws Exception {
        PropertyArrearageLinkageVO vo = iPropertyArrearageLinkageDao.getArrearageLinkageInfo(dto.getProjectId());
        if (vo != null) {
            //云对讲访客和人脸限制有更改 发送更新项目指令
            if (!vo.getIntercomFaceLimit().equals(dto.getIntercomFaceLimit()) || !vo.getIntercomVisitorLimit().equals(dto.getIntercomVisitorLimit())) {
                //项目信息关键变更需通知云对讲
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("projectId",dto.getProjectId());
                List<DeviceBusinessChangeLogPO> changeLogPOS = new ArrayList<>();
                changeLogPOS.add(new DeviceBusinessChangeLogPO(SmartCommunityEnum.PUSH_PROJECT, jsonObject, dto.getProjectId()));
                JSONObject dataJs = new JSONObject();
                dataJs.put("data", changeLogPOS);
                messageSender.send(
                        RabbitMQConstant.RABBITMQ_HANDLE_SMART_COMMUNITY_ORDERS_QUEUE,
                        JSON.toJSONString(dataJs),
                        RabbitMQConstant.RABBITMQ_SMART_COMMUNITY_ORDERS_EXCHANGE,
                        RabbitMQConstant.RABBITMQ_SMART_COMMUNITY_ORDERS_ROUTING_KEY
                );
            }
        }
        Map<String,Object> params = SqlUtil.convertBeanToMapParams(dto);
        iPropertyArrearageLinkageDao.updateArrearageLinkageInfo(params);
        return vo;
    }

    /**
     * <desc>
     *      更新欠费联动信息
     * <desc/>
     *
     * @param projectId 项目编号
     * @return 欠费者电话号
     * @author Qiang.S
     * @createDate 2019/01/22
     */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public List<String> getArrearagePhoneNum(String projectId) throws Exception {
        return iPropertyArrearageLinkageDao.getArrearagePhoneNum(projectId);
    }

}
